package tgc.edu.tms.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.TablePageable;
import tgc.edu.tms.custom.TimeUtils;
import tgc.edu.tms.entity.Callnames;
import tgc.edu.tms.entity.FlowTable;
import tgc.edu.tms.entity.Student;
import tgc.edu.tms.entity.SysRole;
import tgc.edu.tms.entity.SysUser;
import tgc.edu.tms.entity.TbMenu;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.security.UserUtils;
import tgc.edu.tms.service.FlowTableService;
import tgc.edu.tms.service.StudentService;
import tgc.edu.tms.service.SysRoleService;
import tgc.edu.tms.service.SysUserService;
import tgc.edu.tms.service.TbMenuService;
import tgc.edu.tms.service.TeacherService;

@Controller
public class MainController {
	@Autowired
	private TbMenuService menuService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TeacherService teahcerService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private FlowTableService flowTableService;
	@Autowired
	private SysUserService userDAO;
	@Autowired
	private SysRoleService sysRole;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String index(ModelMap map) {
		return "main/login";
	}
	
	@RequestMapping(value= {"/main", "/"})
	public String main() {
		return "main/main";
	}
	@RequestMapping(value= {"/index"})
	public String index() {
		return "main/index";
	}
	@RequestMapping(value= {"/shiyan"})
	public String shiyan() {
		return "main/shiyan";
	}
	
	/***
	 * 跳转修改密码页面
	 * @return
	 */
	@RequestMapping(value= {"/changePwd"})
	public String changePwd() {
		return "main/changePwd";
	}
	
	/**
	 * 判断原密码是否正确
	 * */
	@RequestMapping(value="/pwd")
	@ResponseBody
	public Boolean pwd(String password) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		boolean f = encoder.matches(password,user.getPassword());
		return f;
	}
	
	/****
	 * 修改密码
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/changePwd1")
	@ResponseBody
	public Boolean changePwd1(String password) {
		try {
			SysUser user = userUtils.getUser();
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			user.setPassword(encoder.encode(password));
			userDAO.save(user);
			boolean f = true;
			return f;
		} catch (Exception e) {
			boolean f = false;
			return f;
		}
		
	}
	
	/***
	 * 个人信息
	 * */
	@RequestMapping(value= {"/message"})
	public String message(ModelMap map) {
		SysUser user = userUtils.getUser();
		List<SysRole> roles = user.getRoles();
		SysRole code = sysRole.findbyCode("ROLE_STUDENT");
		if(roles.contains(code)) {
			Student student = studentService.findById(user.getId());
			map.put("model", student);
		}else {
			Teacher teacher = teahcerService.findById(user.getId());
			map.put("model", teacher);
		}
		return "main/message";
	}
	
	
	/***
	 * 实时课表加载
	 * @param pageParam
	 * @return
	 */
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageParam) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<FlowTable> spec = new Specification<FlowTable>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<FlowTable> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				rules.add(cb.equal(root.get("time"), TimeUtils.getStringTime()));
				rules.add(cb.equal(root.get("jc"), TimeUtils.getJc(TimeUtils.getTime())));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		List<FlowTable> page = flowTableService.findAll(spec);
	    return page;	
	}
	
	
	
	@RequestMapping(value="/adminMain")
	public String adminMain(ModelMap map) {
		//智能新增学年学期
		Integer type=0;
		List<TbMenu> menus= menuService.findByType(type);
		map.put("menus", menus);
		return "main/adminMain2";
	}

}
